//
//  ServiceListTableViewCell.m
//  Contractor
//
//  Created by 金鑫 on 16/5/23.
//  Copyright © 2016年 star j. All rights reserved.
//

#import "ServiceListTableViewCell.h"
#import "CONST.h"

@implementation ServiceListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        
        CGFloat labelwidth = __kScreenWidth / 4 ;
        UILabel *hejiLable = [self creatlabel:14.0f color:[UIColor blueColor] alignment:NSTextAlignmentLeft] ;
        hejiLable.frame = CGRectMake(0, 3, __kScreenWidth*3/8, 16.0f) ;
        [self.contentView addSubview:hejiLable] ;
        self.hejiLable  = hejiLable ;
        
        UILabel *serviceLabel = [self creatlabel:14.0f color:[UIColor blueColor] alignment:NSTextAlignmentCenter] ;
        serviceLabel.frame = CGRectMake(labelwidth, 3, labelwidth, 16.0f) ;
        [self.contentView addSubview:serviceLabel] ;
        self.serviceLable = serviceLabel ;
        
        UILabel *numLabel = [self creatlabel:14.0f color:[UIColor blueColor] alignment:NSTextAlignmentCenter] ;
        numLabel.frame = CGRectMake(labelwidth*2, 3, labelwidth, 16.0f) ;
        [self.contentView addSubview:numLabel] ;
        self.numLable = numLabel ;
        
        UILabel *priceLabel = [self creatlabel:14.0f color:[UIColor redColor] alignment:NSTextAlignmentLeft] ;
        priceLabel.frame = CGRectMake(labelwidth*3, 3, labelwidth, 16.0f) ;
        [self.contentView addSubview:priceLabel] ;
        self.priceLabel = priceLabel ;
        
        UIImageView *imageView = [[UIImageView alloc] init] ;
        [imageView setImage:[UIImage imageNamed:@"layout_small_line_bg.png"]] ;
        imageView.frame = CGRectMake(10, 20, __kScreenWidth - 20, 1) ;
        [self.contentView addSubview:imageView] ;
        self.lineView = imageView ;
        
    }
    return self ;
}

//创建label
- (UILabel *)creatlabel:(CGFloat) size color:(UIColor *)color alignment:(NSTextAlignment) alignment
{
    UILabel *label = [[UILabel alloc] init] ;
    label.font = [UIFont fontWithName:@"AvenirLTStd-Light" size:size] ;
    label.backgroundColor = [UIColor clearColor] ;
    label.textAlignment = alignment ;
    label.textColor = color ;
    return label ;
}

- (CGSize)lengthwithLabel:(UILabel *)lable withsize:(CGFloat) size
{
    NSDictionary *attribute1 = @{NSFontAttributeName: [UIFont fontWithName:@"AvenirLTStd-Light" size:size]};
    CGSize DescSize = [lable.text boundingRectWithSize:CGSizeMake(MAXFLOAT, size) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute1 context:nil].size;
    return DescSize ;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"cellID" ;
    ServiceListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier] ;
    if (!cell) {
        cell = [[ServiceListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;
    }
    return cell ;
}

- (void)config:(DetailSheetModle *)model
{
    self.hejiLable.text = [self goodWithStr:model.goods_sku];
    self.serviceLable.text = model.goodsservice ;
    self.numLable.text = [NSString stringWithFormat:@"%@件",model.servicenum] ;
    self.priceLabel.text = [NSString stringWithFormat:@"￥%ld",(long)model.commisionprice] ;
}

- (NSString *)goodWithStr:(NSString *)str
{
    NSMutableArray *intArray = [NSMutableArray array] ;
    for (NSInteger i = 0; i < str.length; i++) {
        NSString *temp = [str substringWithRange:NSMakeRange(i, 1)] ;
        NSLog(@"temp:%@",temp) ;
        NSLog(@"i:%ld",(long)i) ;
        if ([temp isEqualToString:@"|"]) {
            NSNumber *num = [NSNumber numberWithInteger:i] ;
            [intArray addObject:num] ;
        }
    }
    
    NSLog(@"intArray:%lu",(unsigned long)intArray.count) ;
    if (intArray.count != 0) {
        NSInteger first = [intArray[0] integerValue];
        NSInteger end = [intArray[1] integerValue] ;
        
        str = [str substringWithRange:NSMakeRange(first+1, end - first-1)] ;
    }
    else{
        str = @"" ;
    }
    
    return [NSString stringWithFormat:@"   %@",str];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

/*
 CGFloat labelwidth = __kScreenWidth / 4 ;
 UILabel *hejiLable = [self creatlabel:14.0f color:[UIColor blackColor] alignment:NSTextAlignmentLeft] ;
 hejiLable.text = @"   合计" ;
 hejiLable.frame = CGRectMake(0, frame.origin.y + frame.size.height + 3, labelwidth, 16.0f) ;
 [ScrollView addSubview:hejiLable] ;
 
 UILabel *serviceLabel = [self creatlabel:14.0f color:[UIColor blackColor] alignment:NSTextAlignmentCenter] ;
 serviceLabel.text = @"服务" ;
 serviceLabel.frame = CGRectMake(labelwidth, frame.origin.y + frame.size.height + 3, labelwidth, 16.0f) ;
 [ScrollView addSubview:serviceLabel] ;
 
 UILabel *numLabel = [self creatlabel:14.0f color:[UIColor blackColor] alignment:NSTextAlignmentCenter] ;
 numLabel.text = [NSString stringWithFormat:@"%@件",detailModel.servicenum] ;
 numLabel.frame = CGRectMake(labelwidth*2, frame.origin.y + frame.size.height + 3, labelwidth, 16.0f) ;
 [ScrollView addSubview:numLabel] ;
 
 UILabel *priceLabel = [self creatlabel:14.0f color:[UIColor blackColor] alignment:NSTextAlignmentCenter] ;
 priceLabel.text = [NSString stringWithFormat:@"￥%@",detailModel.serviceprice] ;
 priceLabel.frame = CGRectMake(labelwidth*3, frame.origin.y + frame.size.height + 3, labelwidth, 16.0f) ;
 [ScrollView addSubview:priceLabel] ;
 */

@end
